package com.sunshine.util;

import android.content.Context;

import com.birbit.android.jobqueue.Job;
import com.birbit.android.jobqueue.JobManager;
import com.birbit.android.jobqueue.TagConstraint;
import com.birbit.android.jobqueue.config.Configuration.Builder;

/**
 * Description:
 * File: AzfJobManager.java
 * Create: 2019/02/15 09:42
 *
 * @Author: Administrator
 */
public class AzfJobManager {
    private JobManager jobManager;

    public AzfJobManager(Context context) {
        initJobManager(context);
    }

    /**
     * @param context Context
     */
    private void initJobManager(Context context) {
        if (this.jobManager == null) {
            Builder builder = new Builder(context);
            builder.maxConsumerCount(1);
            this.jobManager = new JobManager(builder.build());
        }
    }

    /**
     * @return JobManager
     */
    public JobManager getJobManager() {
        if (this.jobManager != null) {
            return jobManager;
        }
        return null;
    }

    public void addJob(Job job) {
        if (jobManager == null) {
            LogUtil.log("addJob jobManager null");
            return;
        }
        jobManager.addJobInBackground(job);
    }

    public void removeJob(String tag) {
        if (jobManager == null) {
            LogUtil.log("removeJob jobManager null");
            return;
        }
        jobManager.cancelJobsInBackground(null, TagConstraint.ANY, tag);
    }

    /**
     * @return 返回job的数量
     */
    public int getCount() {
        if (null == jobManager) {
            return 0;
        }
        return jobManager.count();
    }
}
